package com.tools.cleanmaster.ui.activity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.cpu.time.AppRunTimeConfig;
import com.hm.ads.sdk.HmAdsSDK;
import com.hm.base.android.mob.AMApplication;
import com.hm.base.android.mob.util.StringUtil;
import com.tools.ad.ADConstant;
import com.tools.ad.EntranceType;
import com.tools.cleanmaster.BuildConfig;
import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.utils.Reflect;
import com.tools.cleanmaster.utils.ReflectException;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * desc: Test activity
 * date: 2017/12/18 14:08
 * author: ancun
 */
public class CleanerActivity extends ListActivity implements AdapterView.OnItemClickListener {


    private CleanApplication imContext;
    private EditText mChannelNameEt;

    public interface FuncExecute {
        void action();
    }

    private List<String> mFuncNameList = new ArrayList<String>();
    private Map<String, FuncExecute> mFuncMap = new LinkedHashMap<String, FuncExecute>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cleaner);
        mChannelNameEt = (EditText) findViewById(R.id.editText);

        initFunctionMap();

        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mFuncNameList);
        ListView listview = getListView();
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(this);

        imContext = (CleanApplication) AMApplication.getInstance();
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String functionName = mFuncNameList.get(position);
        FuncExecute execute = mFuncMap.get(functionName);
        execute.action();
    }

    private void initFunctionMap() {
        mFuncMap.put("点击切换为自定义渠道： (当前:" + HmAdsSDK.getInstance().getReferrerChannel() + ")", new FuncExecute() {
            @Override
            public void action() {
                String channelName = mChannelNameEt.getText().toString();
                if (!StringUtil.isEmptyString(channelName)) {
                    HmAdsSDK.getInstance().setReferrerChannel(channelName);

                    if ("organic".equals(channelName)) {
                        imContext.getADModule().getADSharedPrefManager().setSwipeAdEnable(false);
                    }
                }

            }
        });
        mFuncMap.put("场景化测试入口", new FuncExecute() {
            @Override
            public void action() {
                Intent intent = new Intent(CleanerActivity.this, SceneTestActivity.class);
                startActivity(intent);
            }
        });
        mFuncMap.put("应用运行时间快进 12 小时", new FuncExecute() {
            @Override
            public void action() {
                AppRunTimeConfig.setAppUsedTime(AppRunTimeConfig.getAppUsedTime() + 12 * DateUtils.HOUR_IN_MILLIS);
            }
        });
        mFuncMap.put("YYW 间隔快进 1 小时", new FuncExecute() {
            @Override
            public void action() {
                String key = EntranceType.APP_OUTER_AD.getName() + ADConstant.AdSharedPrefKey.SHOW_TIME;
                long lastShowTime = imContext.getADModule().getADBeanManager().getADSharedPrefManager().getLastShowTime(key);
                long overLastShowTime = lastShowTime - DateUtils.HOUR_IN_MILLIS;
                imContext.getADModule().getADBeanManager().getADSharedPrefManager().setLastShowTime(key, overLastShowTime);
            }
        });
        mFuncMap.put("AdMob Mediation", new FuncExecute() {
            @Override
            public void action() {
                if (BuildConfig.DEBUG) {
                    try {
                        Reflect.on("com.google.android.ads.mediationtestsuite.MediationTestSuite")
                                .call(
                                        "launch",
                                        CleanerActivity.this,
                                        ADConstant.ADMOB_APP_KEY
                                );
                    } catch (ReflectException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        mFuncNameList.addAll(mFuncMap.keySet());
    }


}
